<?php

class Admin_MarkController extends ExtLb_Controller_Action
{
	private $mark;
	
	private $markForm;
	private $markTbl;
	private $lop;
	private $mon;
	private $student;
	private $manager;
	
	private $auth;
	private $storage;
	
	private $markSessionSet;
	private $markSessionGet;

    public function init()
    {
        $this->markForm = new Admin_Form_MarkForm();
        $this->markTbl 	= new Admin_Model_Mark();
        $this->lop		= new Admin_Model_Lops();
        $this->mon		= new Admin_Model_Monhoc();
        $this->student	= new Admin_Model_Student();
        $this->manager	= new Admin_Model_Manages();
        
        $this->markSessionSet =new Zend_Session_Namespace('markSess');
        $this->refreshSession('markSess');
        
        //
        // === Get info Login
        $this->auth = Zend_Auth::getInstance();
        $this->storage = $this->auth->getStorage()->read();
        
        $this->loadLayout('admin', 'layout');
        $this->loadCss('admin', 'mark');
        
    }

    public function indexAction(){

    	$this->refreshSession();
    	
    	//
	    // === LOC LOP THEO KHOA HOC
	    if((isset($_POST['tbn_filter']) && $_POST['tbn_cate'] != 0)
	    		|| !empty($this->markSessionGet['khoaHoc'])){
	    		
    		// === SESSION
    		$this->markSessionSet->khoaHoc	= isset($_POST['tbn_cate']) ? $_POST['tbn_cate'] : $this->markSessionGet['khoaHoc'];
    		$this->refreshSession();
    		
    		$_REQUEST['khoa'] = $khoa = isset($_POST['tbn_cate']) ? $_POST['tbn_cate'] : $this->markSessionGet['khoaHoc'];
    		
    		if ($this->storage->quyen == "admin"){
    			$lops = $this->lop->getAllLop("khoa = '".$khoa."'");
    		}else{
    			$lops = $this->manager
    					->selectManager("khoa = '".$khoa."' and u.username = '".$this->storage->username."'")
    					->query()->fetchAll();	
    		}
    		
    		// === HUY SESSION (khi click lai nut loc lop theo khoa
    		if(isset($_GET['tbn_filter'])){
    			unset($this->markSessionSet->lop);
    			$this->refreshSession();
    		}
    	} else {// End if FILTER
    		
    		if ($this->storage->quyen == "admin"){
    			$lops = $this->lop->getAllLop();
    				
    		}else{// === Proccess with "giaovien" role
    			$lops = $this->manager
    					->selectManager("u.username = '".$this->storage->username."'")
    					->query()
    					->fetchAll();
    		}
    	}
	    //		/
    	// === Get all idLop cua giao quyen co quyen quan ly de load len combobox
    	foreach ($lops as $valueLop) {
    		$idLopArr[] = $valueLop['id'];
    	}
    	
    	if(isset($_POST['cmdView'])){
    		$idLop 		= $this->_getParam('cbbLop');
    		$idMonHoc	= $this->_getParam('cbbMon');

        	//
	    	// === Proccess with role admin
    		if ($this->storage->quyen == "admin"){
    			
    		}else{// === Proccess with "giaovien" role
    			
    		}
    			
    		$selectStudentMark = $this->student->selectStudent(
						"idLop 	 = '".$idLop."' and
						idMonHoc = ".$idMonHoc
					);

			
	    }else{ // === End POST
	    		$selectStudentMark = $this->student->selectStudent();
	    }
	    
    	// === Tim theo ten
    	if(isset($_GET['search_box_sumit']) && !empty($_GET['search_box_text'])){
    		$selectStudentMark = $this->student->selectStudent(
						"ten like '%".$_GET['search_box_text']."%' or
						hoDem like '%".$_GET['search_box_text']."%'"
				);
    	}
	    		
    	
	    	
    	//PHAN TRANG
		    $paginatorMark = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($selectStudentMark));
		    $paginatorMark->setItemCountPerPage(20)
    					->setPageRange(3)
    					->setCurrentPageNumber($this->_getParam('page'),1);
    			
    	//VIEW
    	$this->view->paginatorMark 	= $paginatorMark;
    	$this->view->khoaHocs 		= $this->lop->getKhoaHoc();
    	$this->view->lops			= $lops;
    	
    	///
    	/// ==== Get mon hoc to load combbox
    	if ($this->storage->quyen != "admin"){
    		$idMonArr	 = $this->manager->fetchCol(array('distinct idMonHoc'),"username ='".$this->storage->username."'");

    		$where ="";
    		foreach ($idMonArr as $idMon) {
    			$where .=" id =".$idMon." or ";
    		}
    		$where = substr($where, 0,strlen($where) -3);
    		
    		$this->view->mons			= $this->mon->fetchAllMonHoc($where);
    	}else{
    		$this->view->mons			= $this->mon->fetchAllMonHoc();
    	}
    	 
    }

    public function deleteAction() {
    	
        // action body
    }

    public function editAction()
    {
    	if($this->markForm->isValid($this->_request->getPost())){
	    	if(isset($_POST['cmdEdit'])){
				$user 		= $this->_getParam('txtUser');
	        	$idMon 		= $this->_getParam('cbbMon');
	        	$diemHeSo1 	= $this->_getParam('txtMarkHeSo1');
	        	$diemHeSo2 	= $this->_getParam('txtMarkHeSo2');
	        	$diemThi 	= $this->_getParam('txtMarkThi');
	        	$idLop		= $this->_getParam('cbbLop');
	        	$hocKy		= $this->_getParam('cbbHocKy');
	        	$namHoc		= $this->_getParam('txtNamHoc');
	        	
	        	
	        	$this->markTbl->updateMark($user, $idMon,$diemHeSo1,$diemHeSo2,$diemThi,$hocKy,$namHoc);
	        	
	        	$this->jShowMessage("Thành công!!!");
	        	$this->_forward('index','mark','admin');
	        }	
    	}else{
    		$this->_forward('index','mark','admin');
    	}
        
    }
    
    public function addAction(){
    	if($this->markForm->isValid($this->_request->getPost())){
    		if(isset($_POST['cmdAdd'])){
    			$user 		= $this->_getParam('txtUser');
	        	$idMon 		= $this->_getParam('cbbMon');
	        	$diemHeSo1 	= $this->_getParam('txtMarkHeSo1');
	        	$diemHeSo2 	= $this->_getParam('txtMarkHeSo2');
	        	$diemThi 	= $this->_getParam('txtMarkThi');
	        	$idLop		= $this->_getParam('cbbLop');
	        	$hocKy		= $this->_getParam('cbbHocKy');
	        	$namHoc		= $this->_getParam('txtNamHoc');
	        	
	        	$this->markTbl->addMark($user, $idMon,$diemHeSo1,$diemHeSo2,$diemThi,$hocKy,$namHoc);
	        	
	        	$this->jShowMessage("Thành công!!!");
	        	$this->_forward('index','mark','admin');
    		}
    	}
    }
    public function inputmarkAction(){
    	//GET DATA TO EDIT
    	if(isset($_GET['lop']) && isset($_GET['mon']) && isset($_GET['user'])){
    		
    		
    		$this->markForm->addAction('edit');
    		$this->markForm->addSubmitButton('cmdEdit', 'Cập nhập');
    		//
    		$this->markForm->addHiddenId('txtUser', $_GET['user']);

    		// === If chua sv chua có điểm của môn X thì nhập bằng tay
    		if(empty($_GET['mon'])){
    			$dataMark = $this->student->selectStudent(
	    			"sv.idLop   = '".$_GET['lop']."' and
	    			sv.username = '".$_GET['user']."'"
	    		)->query()->fetchAll();
    		}else{
	    		$dataMark = $this->markTbl->listMark(
	    			"idLop   = '".$_GET['lop']."' and
	    			idMonHoc = ".$_GET['mon']."   and
	    			d.username = '".$_GET['user']."'"
	    		)->query()->fetchAll();
    		} // === End if
    		
    		
    		$this->markForm->getElement('txtHoDem')->setValue($dataMark[0]['hoDem']);
    		$this->markForm->getElement('txtName')->setValue($dataMark[0]['ten']);
    		$this->markForm->getElement('txtMarkHeSo1')->setValue($dataMark[0]['diemHeSo1']);
    		$this->markForm->getElement('txtMarkHeSo2')->setValue($dataMark[0]['diemHeSo2']);
    		$this->markForm->getElement('txtMarkThi')->setValue($dataMark[0]['diemThi']);
    		$this->markForm->getElement('txtNamHoc')->setValue($dataMark[0]['namHoc']);
    		
    	}else{
	    	$this->markForm->addAction('add');
	    	$this->markForm->addSubmitButton('cmdAdd', 'Nhập điểm');
    	}
    	$this->view->markForm = $this->markForm; 
    }

	private function refreshSession($nameSession="markSess"){
    	$this->markSessionGet = Zend_Session::namespaceGet($nameSession);
    }
}







